package cn.jieruoda.eb.store.service.impl;

import cn.jieruoda.eb.store.domain.entity.StoreProduct;
import cn.jieruoda.eb.store.domain.repository.StoreProductRepository;
import cn.jieruoda.eb.store.service.StoreProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class StoreProductServiceImpl implements StoreProductService {

    @Autowired
    private StoreProductRepository storeProductRepository;

    @Override
    @Transactional
    public StoreProduct save(StoreProduct product) {
        return storeProductRepository.save(product);
    }

    @Override
    public StoreProduct findById(Integer id) {
        return storeProductRepository.findById(id)
                .orElseThrow(() -> new RuntimeException("商品不存在"));
    }

    @Override
    public Page<StoreProduct> findAll(Pageable pageable) {
        return storeProductRepository.findAll(pageable);
    }

    @Override
    @Transactional
    public void deleteById(Integer id) {
        storeProductRepository.deleteById(id);
    }

    @Override
    @Transactional
    public StoreProduct update(StoreProduct product) {
        if (!storeProductRepository.existsById(product.getId())) {
            throw new RuntimeException("商品不存在");
        }
        return storeProductRepository.save(product);
    }
} 